package com.jinlejia.erp.ui.house;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.VisitRecordAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityVisitRecordBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * 带访记录
 */
public class VisitRecordActivity extends AppCompatActivity {
    VisitRecordAdapter adapter;

    private ActivityVisitRecordBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_visit_record);
        binding.backBtn.setOnClickListener(this::onClick);


        binding.yfRecycler.setHasFixedSize(true);
        binding.yfRecycler.setLayoutManager(new LinearLayoutManager(this));
        ArrayList<Map<String, Object>> data = new ArrayList<>();
        adapter = new VisitRecordAdapter(data,getIntent().getStringExtra("type"));
        binding.yfRecycler.setAdapter(adapter);
        getData();

    }

    public void onClick(View view) {
        finish();
    }

    private void getData(){
        String url;
        if( getIntent().hasExtra("type") && (getIntent().getStringExtra("type").equals("sale") || getIntent().getStringExtra("type").equals("shs"))){
            url = ApiConfig.SHS_VISIT_RECORD_LIST;
        }else{
            url = ApiConfig.RENT_VISIT_RECORD_LIST;
        }
        OkHttpUtils.get()
                .url(url.concat(getIntent().getStringExtra("code")))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(VisitRecordActivity.this, response,adapter)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            ArrayList<Map<String, Object>> list = JSON.parseObject(data.getJSONArray("viewResultList").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            adapter.setData(list);
                        }
                    }
                });
    }
}
